package com.antcom.antread.antreaddatacrawl.himalayaData.utils.vipEAD;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.ArrayList;
import java.util.List;

/**
 *  vip解密参数
 * @Author 中国蚂蚁技术有限公司
 * @create 2020/9/17 19:02
 */
public class VipEncryption {

    public static ScriptEngineManager manager = new ScriptEngineManager();

    /**
     * 获取JavaScript执行引擎
     */
    public static ScriptEngine engine = manager.getEngineByName("js");
    /**
     * 加密路径js方法
     */
    private static String pathStr = "function vt(t) {\n" +
            "    this._randomSeed = t,\n" +
            "        this.cg_hun()\n" +
            "}\n" +
            "vt.prototype = {\n" +
            "    cg_hun: function() {\n" +
            "        this._cgStr = \"\";\n" +
            "        var t = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\\\:._-1234567890\"\n" +
            "            , e = t.length\n" +
            "            , n = 0;\n" +
            "        for (n = 0; n < e; n++) {\n" +
            "            var r = this.ran() * t.length\n" +
            "                , o = parseInt(r);\n" +
            "            this._cgStr += t.charAt(o),\n" +
            "                t = t.split(t.charAt(o)).join(\"\")\n" +
            "        }\n" +
            "    },\n" +
            "    cg_fun: function(t) {\n" +
            "        t = t.split(\"*\");\n" +
            "        var e = \"\"\n" +
            "            , n = 0;\n" +
            "        for (n = 0; n < t.length - 1; n++)\n" +
            "            e += this._cgStr.charAt(t[n]);\n" +
            "        return e\n" +
            "    },\n" +
            "    ran: function() {\n" +
            "        this._randomSeed = (211 * this._randomSeed + 30031) % 65536;\n" +
            "        return this._randomSeed / 65536\n" +
            "    }\n" +
            "};\n" +
            "function c(t, e) {\n" +
            "    var n = new vt(t).cg_fun(e);\n" +
            "    return \"/\" === n[0] ? n : \"/\".concat(n)\n" +
            "}";


    /**
     *  加密算数js方法
     */
    private static String paramStr ="Z = function() {\n" +
            "                throw new TypeError(\"Invalid attempt to destructure non-iterable instance\")\n" +
            "            }\n" +
            "            \n" +
            "J = function(t, e) {\n" +
            "var n = []\n" +
            "  , r = !0\n" +
            "  , o = !1\n" +
            "  , i = void 0;\n" +
            "try {\n" +
            "    for (var a, u = t[Symbol.iterator](); !(r = (a = u.next()).done) && (n.push(a.value),\n" +
            "    !e || n.length !== e); r = !0)\n" +
            "        ;\n" +
            "} catch (t) {\n" +
            "    o = !0,\n" +
            "    i = t\n" +
            "} finally {\n" +
            "    try {\n" +
            "        r || null == u.return || u.return()\n" +
            "    } finally {\n" +
            "        if (o)\n" +
            "            throw i\n" +
            "    }\n" +
            "}\n" +
            "return n\n" +
            "}\n" +
            "\n" +
            "Q = function(t) {\n" +
            "if (Array.isArray(t))\n" +
            "    return t\n" +
            "}\n" +
            "\n" +
            "tt = function(t, e) {\n" +
            "    return Q(t) || J(t, e) || Z()\n" +
            "}\n" +
            "\n" +
            "\n" +
            "function yt(t, e) {\n" +
            "    for (var n, r = [], o = 0, i = \"\", a = 0; 256 > a; a++)\n" +
            "        r[a] = a;\n" +
            "    for (a = 0; 256 > a; a++)\n" +
            "        o = (o + r[a] + t.charCodeAt(a % t.length)) % 256,\n" +
            "        n = r[a],\n" +
            "        r[a] = r[o],\n" +
            "        r[o] = n;\n" +
            "    for (var u = o = a = 0; u < e.length; u++)\n" +
            "        o = (o + r[a = (a + 1) % 256]) % 256,\n" +
            "        n = r[a],\n" +
            "        r[a] = r[o],\n" +
            "        r[o] = n,\n" +
            "        i += String.fromCharCode(e.charCodeAt(u) ^ r[(r[a] + r[o]) % 256]);\n" +
            "    return i\n" +
            "}\n" +
            "\n" +
            "var mt = yt(\"xm\", \"Ä[Ü\u0014J=\u0086Û3Áf÷\u0017N\")\n" +
            " gt = [19, 1, 4, 7, 30, 14, 28, 8, 24, 17, 6, 35, 34, 16, 9, 10, 13, 22, 32, 29, 31, 21, 18, 3, 2, 23, 25, 27, 11, 20, 5, 15, 12, 0, 33, 26]\n" +
            "\n" +
            "bt = function(t) {\n" +
            "\n" +
            "var e1 = yt(\n" +
            "    function(t, e) {\n" +
            "    for (var n = [], r = 0; r < t.length; r++) {\n" +
            "        for (var o = \"a\" <= t[r] && \"z\" >= t[r] ? t[r].charCodeAt() - 97 : t[r].charCodeAt() - \"0\".charCodeAt() + 26, i = 0; 36 > i; i++)\n" +
            "            if (e[i] == o) {\n" +
            "                o = i;\n" +
            "                break\n" +
            "            }\n" +
            "        n[r] = 25 < o ? String.fromCharCode(o - 26 + \"0\".charCodeAt()) : String.fromCharCode(o + 97)\n" +
            "    }\n" +
            "    return n.join(\"\")\n" +
            "    }(\"d\" + mt + \"9\",gt)\n" +
            "    ,\n" +
            "    e2 = function(t) {\n" +
            "        if (!t)\n" +
            "            return \"\";\n" +
            "        var e, n, r, o, i, a = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];\n" +
            "        for (o = (t = t.toString()).length,\n" +
            "        r = 0,\n" +
            "        i = \"\"; r < o; ) {\n" +
            "            do {\n" +
            "                e = a[255 & t.charCodeAt(r++)]\n" +
            "            } while (r < o && -1 == e);if (-1 == e)\n" +
            "                break;\n" +
            "            do {\n" +
            "                n = a[255 & t.charCodeAt(r++)]\n" +
            "            } while (r < o && -1 == n);if (-1 == n)\n" +
            "                break;\n" +
            "            i += String.fromCharCode(e << 2 | (48 & n) >> 4);\n" +
            "            do {\n" +
            "                if (61 == (e = 255 & t.charCodeAt(r++)))\n" +
            "                    return i;\n" +
            "                e = a[e]\n" +
            "            } while (r < o && -1 == e);if (-1 == e)\n" +
            "                break;\n" +
            "            i += String.fromCharCode((15 & n) << 4 | (60 & e) >> 2);\n" +
            "            do {\n" +
            "                if (61 == (n = 255 & t.charCodeAt(r++)))\n" +
            "                    return i;\n" +
            "                n = a[n]\n" +
            "            } while (r < o && -1 == n);if (-1 == n)\n" +
            "                break;\n" +
            "            i += String.fromCharCode((3 & e) << 6 | n)\n" +
            "        }\n" +
            "        return i\n" +
            "    }(t)\n" +
            "    ).split(\"-\");\n" +
            "  var str = \"\";\n" +
            "  for(var i = 0; i < e1.length; i++) {\n" +
            "    str+=e1[i]+\",\";\n" +
            "\t}\n" +
            "  return str;\n" +
            "}";


    /**
     * 解密js路径方法
     * @param seed
     * @param fileId
     * @return
     */
    public static String pathDec(Integer seed,String fileId) {

        try {
            engine.eval(pathStr);
            Invocable inv = (Invocable)engine;
            String eval = (String) inv.invokeFunction("c", seed,fileId);
            return eval;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密参数方法
     * @param ep
     * @return
     */
    public static List<String> paramDec(String ep) {
        List<String> list = new ArrayList<>();
        try {
            engine.eval(paramStr);
            Invocable inv = (Invocable)engine;
            String bt = (String) inv.invokeFunction("bt", ep);
            String epEn = bt.substring(0, bt.length() - 1);
            String[] split = epEn.split(",");
            for (int i = 0; i < split.length; i++) {
                list.add(split[i]);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
